<?php
/**
 * Created by PhpStorm.
 * User: 56667
 * Date: 2019/5/18
 * Time: 21:51
 */

namespace dtapi;


class tpl
{
    public $data = []; //接收控制器传过来的变量

    public static function template($template = 'index', $dir = '') {
        global $CFG,$DTapi;
        extract($GLOBALS, EXTR_SKIP);
        check_name($template) or exit('BAD TPL NAME');
        $appdir = '/dtapi/'.$DTapi['author'].'/'.$DTapi['name'].'/';
        $to = DT_CACHE.'/tpl/'.$appdir.($dir ? $dir.'/' : '').$template.'.php';
        $isfileto = is_file($to);
        if($CFG['template_refresh'] || !$isfileto) {
            $fromdir = DTAPI_APP_ROOT.'tpl/';
            if($dir) $fromdir .= $dir;
            $from = $fromdir.$template.'.htm';
            if(!$isfileto || filemtime($from) > filemtime($to) || (filesize($to) == 0 && filesize($from) > 0)) {
                require_once DT_ROOT.'/include/template.func.php';
                template_compile($from, $to);
            }
        }

        return $to;
    }

    public function __get($tpl_name){
        extract($GLOBALS, EXTR_SKIP);
        if(!empty($this->data)) extract($this->data,EXTR_SKIP);
        return include self::template($tpl_name);
    }

    public function html($tpl_name,$tpl_dir=''){
        extract($GLOBALS, EXTR_SKIP);
        if(!empty($this->data)) extract($this->data,EXTR_SKIP);
        return include $this->template($tpl_name,$tpl_dir);
    }




}